{% extends "base/base.html" %}
{% block css %} 
<link rel="stylesheet" href="/static/css/back.css">
{% endblock %}
{% block content %}
<h3 class="title">添加视频</h3>
<div class="list-box">
	<a href="/my" class="btn text">返回视频列表</a>
	<form class="form-box" id="addForm"  enctype="multipart/form-data">
		<div class="form-item">
			<label for="name">视频标题</label>
			<input id="name" name="vname" type="text" placeholder="请输入视频名称"/>
		</div>
		<div class="form-item">
			<label for="type">视频类型</label>
			<select name="vtype" id="type">
				<option value="">--请选择类型--</option>
				<option value="电视">电视</option>
				<option value="电影">电影</option>
				<option value="综艺">综艺</option>
				<option value="动漫">动漫</option>
			</select>
		</div>
		<div class="form-item">
			<label for="desc">视频描述</label>
			<textarea id="desc" name="vdesc" placeholder="视频描述 30~50字"></textarea>
		</div>
		<div class="form-item">
			<label for="imageFile">视频图片</label>
			<input type="file" id="imageFile"/>
		</div>
		<div class="form-item">
			<label for="videoFile">视频内容</label>
			<input type="file" id="videoFile"/>
		</div>
		<div class="btn-item">
			<a class="btn" id="addBtn">上传视频</a>
			<input type="reset" class="btn reset-btn" value="重置">
		</div>
	</form>
</div>
{% endblock %}
{% block script %}
 <script>
	let $addForm = $("#addForm"),$addBtn=$("#addBtn")
	let videoFile = document.getElementById("videoFile")
	let imageFile = document.getElementById("imageFile")
	$addBtn.on("click",function(){
		formData=$addForm.serializeArray()
		data = new FormData()
		formData.map(item=>{
			data.append(item.name,item.value)
		})
		data.append("video",videoFile.files[0])
		data.append("image",imageFile.files[0])
		$.ajax({  
                    type: 'POST',  
                    url: '/api/addVideo', // 你的Python后端接收文件的URL  
                    data: data,  
                    contentType: false, // 不设置内容类型  
                    processData: false, // 不处理发送的数据  
                    success: function(response) {  
                        console.log('Success:', response); 
						res = JSON.parse(response) 
						alert(res.msg) 
						if(res.code == 200){
							location.href="/my"
						}
                        // alert('File and text uploaded successfully!');  
                        // 在这里处理成功后的逻辑，比如清空输入框、显示消息给用户等  
                    },  
                    error: function(xhr, status, error) {  
                        console.error('Error:', error);  
                        alert('Error uploading file and text!');  
                        // 在这里处理错误情况  
                    }  
                });  
		
	})
 </script>
{% endblock %}